<%--
  Created by IntelliJ IDEA.
  User: Jia_ly
  Date: 2016/12/21
  Time: 15:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path;
    request.setAttribute("basePath", basePath);
%>
<html>
<%@ page isELIgnored="false" %>
<%@ include file="../frame/head.jsp" %>
<body>
<section class="vbox">
    <%@include file="../frame/top.jsp" %>
    <section class="boxMain">
        <section class="hbox stretch">
            <%@include file="../frame/aside.jsp" %>
            <section>
                <header>
                    <h3 style="color: #0b6cbc; text-align: center">修改密码</h3>
                </header>
                <hr>
                <div>
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <tbody style="text-align:center">
                            <tr>
                                <td><strong>原密码:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="oldpassword" name="oldpassword" type="text" class="form-control"
                                               placeholder="请输入旧的密码">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td><strong>新密码:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="newpassword" name="newpassword" type="text" class="form-control"
                                               placeholder="请输入新的密码">
                                    </div>
                                </td>
                            </tr>

                            <tr>
                                <td><strong>确认新密码:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="confirmpassword" name="confirmpassword" type="text"
                                               class="form-control"
                                               placeholder="请再次输入新的密码">
                                    </div>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                    <div align="center">
                        <button id="commit" class="btn btn-s-md btn-primary">提交</button>
                    </div>
                </div>
            </section>
        </section>
    </section>
</section>
</body>
<script>
    /*如果没有登录，则跳转到登陆页面*/
    if (<%=session.getAttribute("userName") == null%>) {
        location.href = "${basePath}";
    }
</script>
<%--提交按钮--%>
<script>
    function judge() {
        var newpasswd = $("#newpassword").val();
        var confirmpasswd = $("#confirmpassword").val();
        var oldpasswd = $("#oldpassword").val();
        if ((newpasswd == "") || (confirmpasswd == "") || (oldpasswd == "") ||
                (newpasswd != confirmpasswd)) {
            layer.alert("新密码不一致或有内容没有填写")
            var txt = document.getElementsByTagName("input");
            for (i = 0; i < txt.length; i++) {
                if (txt[i].type == 'text') {
                    txt[i].value = "";
                }
            }
            return false;
        }
        else {
            return true;
        }
    }

    $("#commit").click(function () {
        if (judge()) {
            $.ajax({
                type: 'POST',
                asyn: false,
                url: "${basePath}/user/updatePassword",
                data: {
                    oldpsw: $("#oldpassword").val(),
                    newpsw: $("#newpassword").val(),
                    confirmpsw: $("#confirmpassword").val()
                },
                dataType: 'JSON',
                success: function (data) {
                    if (true == data) {
                        layer.alert("操作成功");
                    } else {
                       layer.alert("原密码错误");
                    }
                }
            });
        }
        else {

        }
    });
</script>
</html>
